package org.ting.e_commerce.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ting.e_commerce.model.Result;
import org.ting.e_commerce.model.UserAddress;
import org.ting.e_commerce.service.UserAddressService;

import java.util.List;

@RestController
@RequestMapping("/userAddress")
public class UserAddressController {
    @Autowired
    UserAddressService addressService;
    @RequestMapping("/addUserAddress")
    public Result<?> addUserAddress(UserAddress address, String province, String city, String area, String town, String detail) {
        address.setAddressDetail(province + " " + city + " " + area + " " + town + " " + detail);
        if(addressService.addUserAddress(address)) {
            return Result.success("success");
        }
        return Result.fail("添加失败");
    }
    @RequestMapping("/getAddressById")
    public UserAddress getAddressById(int addressId){
        return addressService.getAddressById(addressId);
    }
    @RequestMapping("/getNoDefaultAddress")
    public List<UserAddress> getNoDefaultAddress(int userId) {
        return addressService.getNoDefaultAddress(userId);
    }

    @RequestMapping("/getDefaultAddress")
    public UserAddress getDefaultAddress(int userId) {
        return addressService.getDefaultAddress(userId);
    }
    @RequestMapping("/deleteById")
    public Result<?> deleteById(int addressId) {
        if(addressService.deleteById(addressId)) {
            return Result.success("success");
        }
        return Result.fail("删除失败");
    }

    @RequestMapping("/updateById")
    public Result<?> updateById(UserAddress address) {
        if(addressService.updateById(address)) {
            return Result.success("success");
        }
        return Result.fail("修改失败");
    }
    @RequestMapping("/setDefaultAddress")
    public Result<?> setDefaultAddress(int userId, int addressId) {
        if(addressService.setDefaultAddress(userId, addressId)) {
            return Result.success("success");
        }
        return Result.fail("设置失败");
    }
}
